#pragma once

/* cJSON Types: */
#define cJSON_False 0
#define cJSON_True 1
#define cJSON_NULL 2
#define cJSON_Number 3
#define cJSON_String 4
#define cJSON_Array 5
#define cJSON_Object 6
	
#define cJSON_IsReference 256
#define cJSON_StringIsConst 512

/* The cJSON structure: */
typedef struct _NODE {
	struct _NODE *next,*prev;	/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
	struct _NODE *child;		/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */

	int type;					/* The type of the item, as above. */

	char *valuestring;			/* The item's string, if type==cJSON_String */
	int valueint;				/* The item's number, if type==cJSON_Number */
	double valuedouble;			/* The item's number, if type==cJSON_Number */

	char *string;				/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
}JsonNode;

typedef struct cJSON_Hooks {
      void *(*malloc_fn)(size_t sz);
      void (*free_fn)(void *ptr);
} cJSON_Hooks;


class CJson
{
public:
	CJson(void);
	~CJson(void);


/* Supply malloc, realloc and free functions to cJSON */
void cJSON_InitHooks(cJSON_Hooks* hooks);


/* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
JsonNode *cJSON_Parse(const char *value);
/* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
char  *cJSON_Print(JsonNode *item);
/* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
char  *cJSON_PrintUnformatted(JsonNode *item);
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
char *cJSON_PrintBuffered(JsonNode *item,int prebuffer,int fmt);
/* Delete a cJSON entity and all subentities. */
void   cJSON_Delete(JsonNode *c);

/* Returns the number of items in an array (or object). */
int	  cJSON_GetArraySize(JsonNode *array);
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
JsonNode *cJSON_GetArrayItem(JsonNode *array,int item);
/* Get item "string" from object. Case insensitive. */
JsonNode *cJSON_GetObjectItem(JsonNode *object,const char *string);

/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
const char *cJSON_GetErrorPtr(void);
	
/* These calls create a cJSON item of the appropriate type. */
JsonNode *cJSON_CreateNull(void);
JsonNode *cJSON_CreateTrue(void);
JsonNode *cJSON_CreateFalse(void);
JsonNode *cJSON_CreateBool(int b);
JsonNode *cJSON_CreateNumber(double num);
JsonNode *cJSON_CreateString(const char *string);
JsonNode *cJSON_CreateArray(void);
JsonNode *cJSON_CreateObject(void);

/* These utilities create an Array of count items. */
JsonNode *cJSON_CreateIntArray(const int *numbers,int count);
JsonNode *cJSON_CreateFloatArray(const float *numbers,int count);
JsonNode *cJSON_CreateDoubleArray(const double *numbers,int count);
JsonNode *cJSON_CreateStringArray(const char **strings,int count);

/* Append item to the specified array/object. */
void cJSON_AddItemToArray(JsonNode *array, JsonNode *item);
void	cJSON_AddItemToObject(JsonNode *object,const char *string,JsonNode *item);
void	cJSON_AddItemToObjectCS(JsonNode *object,const char *string,JsonNode *item);	/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object */
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
void cJSON_AddItemReferenceToArray(JsonNode *array, JsonNode *item);
void	cJSON_AddItemReferenceToObject(JsonNode *object,const char *string,JsonNode *item);

/* Remove/Detatch items from Arrays/Objects. */
JsonNode *cJSON_DetachItemFromArray(JsonNode *array,int which);
void   cJSON_DeleteItemFromArray(JsonNode *array,int which);
JsonNode *cJSON_DetachItemFromObject(JsonNode *object,const char *string);
void   cJSON_DeleteItemFromObject(JsonNode *object,const char *string);
	
/* Update array items. */
void cJSON_InsertItemInArray(JsonNode *array,int which,JsonNode *newitem);	/* Shifts pre-existing items to the right. */
void cJSON_ReplaceItemInArray(JsonNode *array,int which,JsonNode *newitem);
void cJSON_ReplaceItemInObject(JsonNode *object,const char *string,JsonNode *newitem);

/* Duplicate a cJSON item */
JsonNode *cJSON_Duplicate(JsonNode *item,int recurse);
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
need to be released. With recurse!=0, it will duplicate any children connected to the item.
The item->next and ->prev pointers are always zero on return from Duplicate. */

/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
JsonNode *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated);

void cJSON_Minify(char *json);

JsonNode *cJSON_Load(const char *strJson_File);
void cJSON_Save(JsonNode *root, const char *strJson_File);
};

